from flask import session
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from info import create_app, db
from info import models
from info.models import User

app = create_app("development")

manager = Manager(app)
Migrate(app, db)  # 将app与db 关联
# 将迁移命令添加到manager中
manager.add_command("db", MigrateCommand)


@manager.option("-n", "-user_name", dest="user_name")
@manager.option("-p", "-password", dest="password")
def createsuperuser(user_name, password):
    if not all([user_name, password]):
        print("参数不足")
    # 创建管理员账户
    user = User()
    user.nick_name = user_name
    user.password = password
    user.mobile = user_name
    user.is_admin = True
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        print(e)
    print("添加成功")

if __name__ == '__main__':
    manager.run()
